package fag.editor;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.event.EventListenerList;

public class FrameModifier extends JPanel implements ActionListener {

	private static final long serialVersionUID = 7383394578901412450L;
	
	private final static int DEFAULT_WIDTH = 250;
	private final static int DEFAULT_HEIGHT = 80;
	
	private JFormattedTextField ftfDuration = new JFormattedTextField(NumberFormat.getIntegerInstance());
	private JButton btAddFrame = new JButton(new ImageIcon(getClass().getResource("/imgs/+.png"),"PlusIcon"));
	private JButton btRemoveFrame = new JButton(new ImageIcon(getClass().getResource("/imgs/-.png"),"MoinsIcon"));
	
	protected final static String ACTION_ADD_FRAME = "Add Frame";
	protected final static String ACTION_REMOVE_FRAME = "Remove Frame";
	protected final static String ACTION_DURATION_CHANGED = "Duration Changed";

	private final EventListenerList listeners = new EventListenerList();
	
	/**
	 * Constructeur par défaut.
	 */
	public FrameModifier() {
		
		setBorder(BorderFactory.createTitledBorder("Frame"));
		
		ftfDuration.setPreferredSize(new Dimension(100,40));
		btAddFrame.setPreferredSize(new Dimension(40,40));
		btRemoveFrame.setPreferredSize(new Dimension(40,40));
		
		add(btAddFrame);
		add(btRemoveFrame);
		add(ftfDuration);
		
		ftfDuration.addActionListener(this);
		btAddFrame.addActionListener(this);
		btRemoveFrame.addActionListener(this);

		repaint();
	}
	
	public int getCurrentDuration() {
		
		return Integer.parseInt(ftfDuration.getText());
	}
	
	public void setCurrentDuration(int i) {
		
		ftfDuration.setText(String.valueOf(i));
	}
	@Override
	public Dimension preferredSize() {
		
		return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}

	public void addActionListener(ActionListener listener) {
		
		listeners.add(ActionListener.class,listener);
	}
	
	public void removeActionListener(ActionListener listener) {
		
		listeners.remove(ActionListener.class, listener);
	}
	
	protected void addFrame() {
		
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_ADD_FRAME));
	}
	
	protected void removeFrame() {
		
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_REMOVE_FRAME));
	}
	
	protected void durationChanged() {
		
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_DURATION_CHANGED));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == ftfDuration)
			durationChanged();
		
		if(e.getSource() == btAddFrame)
			addFrame();
		
		if(e.getSource() == btRemoveFrame)
			removeFrame();
	}
}
